<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/7/27
 * Time: 上午6:46
 */

namespace App\Services;


use App\Models\Apart;
use App\Models\Retire;
use App\Models\User;
use Symfony\Component\Finder\Exception\AccessDeniedException;

class RetireService extends Service
{

    public static function getRetireList($limit, $offset, $order = 'desc', $option = [])
    {
        $query = Retire::where('status', 1);
        if (count($option) && count($option['query'])) {
            foreach ($option['query'] as $search) {
                if ($search['key'] == 'apart_id') {
                    $query = $query->whereIn($search['key'], $search['value']);
                } else {
                    $query = $query->where($search['key'], $search['operator'], $search['value']);
                }
            }
        }
        $total = $query->count();
        $retires = $query->orderBy('id', $order)->limit($limit)->offset($offset)->get();
        $data = [];
        foreach ($retires as $retire) {
            $apart = Apart::where('id', $retire->apart_id)->first();
            $user = User::where('id', $retire->user_id)->first();
            $data[] = [
                'id' => $retire->id,
                'type' => $retire->type,
                'name' => $apart->name,
                'user_id' => $retire->user_id,
                'created_by' => $retire->created_by,
                'rent_user_name' => $user->realname . '（'.$user->number.'）',
                'checkout_at' => $retire->checkout_at,
                'state_show' => self::getRetireState($retire),
                'status' => $retire->status
            ];
        }

        return [
            'total' => $total,
            'rows' => $data,
            'limit' => $limit,
            'offset' => $offset,
            'page' => ceil($total / $limit),
            'pageNumber' => $offset / $limit + 1
        ];
    }

    public static function getRetireDetail($id = null, $contractId = null)
    {
        if ($id) {
            $retire = Retire::where('id', $id)->first();
        } else {
            $retire = $retire = Retire::where('contract_id', $contractId)->where('status', 1)->first();
        }

        if (!$retire) {
            return [];
        }

        $apart = Apart::where('id', $retire->apart_id)->first();
        $user = User::where('id', $retire->user_id)->first();

        return [
            'data' => [
                'id' => $retire->id,
                'type' => $retire->type,
                'apart_id' => $apart->id,
                'name' => $apart->name,
                'user_id' => $user->id,
                'rent_user_name' => $user->realname . '（'.$user->number.'）',
                'is_rent_pay' => $retire->is_rent_pay,
                'is_water_fee_pay' => $retire->is_water_fee_pay,
                'is_electric_fee_pay' => $retire->is_electric_fee_pay,
                'is_gas_fee_pay' => $retire->is_gas_fee_pay,
                'is_device_safe' => $retire->is_device_safe,
                'remark' => $retire->remark,
                'checkout_at' => $retire->checkout_at,
                'status' => $retire->status
            ]
        ];

    }

    public static function saveRetire($data)
    {
        $retire = new Retire();
        if (array_key_exists('id', $data) && $data['id']) {
            $retire = Retire::where('id', $data['id'])->first();
        } else {
            $retire->created_by = $data['created_by'];
            $retire->created_at = date('Y-m-d H:i:s', time());
        }
        foreach ($data as $key => $value) {
            if ($key != 'id' && $key != 'created_by') {
                $retire->$key = $value;
            }
        }

        $retire->updated_at = date('Y-m-d H:i:s', time());
        $retire->save();
    }

    public static function deleteRetire($id)
    {
        $retire = Retire::where('id', $id)->first();
        $retire->status = 0;
        $retire->updated_at = date('Y-m-d H:i:s', time());
        $retire->save();
    }

    public static function createRetire($data)
    {
        $retire = Retire::where('contract_id', $data['contract_id'])->where('apart_id', $data['apart_id'])->where('user_id', $data['user_id'])->where('status', 1)->first();
        if (!$retire) {
            $retire = new Retire();
            $retire->contract_id = $data['contract_id'];
            $retire->apart_id = $data['apart_id'];
            $retire->user_id = $data['user_id'];
            $retire->type = $data['type'];
            $retire->is_rent_pay = $data['is_rent_pay'];
            $retire->is_water_fee_pay = $data['is_water_fee_pay'];
            $retire->is_electric_fee_pay = $data['is_electric_fee_pay'];
            $retire->is_gas_fee_pay = $data['is_gas_fee_pay'];
            $retire->is_device_safe = $data['is_device_safe'];
            $retire->remark = $data['remark'];
            $retire->checkout_at = date('Y-m-d H:i:s', time());
            $retire->created_at = date('Y-m-d H:i:s', time());
            $retire->updated_at = date('Y-m-d H:i:s', time());
            $retire->save();
        } else {
            throw new AccessDeniedException('已发起退房', 4032);
        }
    }

    private static function getRetireState(Retire $retire)
    {
        if (!$retire->is_rent_pay) {
            return '处理中';
        }
        if (!$retire->is_water_fee_pay) {
            return '处理中';
        }
        if (!$retire->is_electric_fee_pay) {
            return '处理中';
        }
        if (!$retire->is_device_safe) {
            return '处理中';
        }
        if (!$retire->is_gas_fee_pay) {
            return '处理中';
        }
        return '已完成';
    }
}